﻿using System.Collections.Generic;
using System.Linq;
using Aluetjen.EmailService.Domain.Models.Message;

namespace Aluetjen.EmailService.Domain.Services
{
    public interface IPipelineProcessorService
    {
        void ProcessMessage(MailMessage message);
    }

    public class PipelineProcessorService : IPipelineProcessorService
    {
        private readonly IEnumerable<IPipelineService> _pipelineServices;

        public PipelineProcessorService(IEnumerable<IPipelineService> pipelineServices)
        {
            _pipelineServices = pipelineServices;
        }

        public void ProcessMessage(MailMessage message)
        {
            var pipelineOrderedByPriority = _pipelineServices.OrderBy(p => p.Priority);

            foreach(var service in pipelineOrderedByPriority)
            {
                service.ProcessMessage(message);
            }
        }
    }
}